package org.iwidget.desktop.model;
import org.iwidget.desktop.ui.WidgetTextArea;
import java.awt.Font;
import java.awt.FontMetrics;
import org.w3c.dom.*;
/**
 *
 * @author Muhammad Hakim A
 */

public class ElementTextArea extends ElementUI
{

    public ElementTextArea()
    {
        alignment = "left";
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        color = "#000000";
        data = "";
        editable = true;
        font = "Helvetica";
        hScrollbar = true;
        scrollDirection = 0;
        scrollPosition = 0;
        scrollSpeed = 30;
        size = 10;
        style = "plain";
        vScrollbar = true;
        name = "TextArea";
    }

    public ElementTextArea(WidgetElement widget)
    {
        super(widget);
        alignment = "left";
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        color = "#000000";
        data = "";
        editable = true;
        font = "Helvetica";
        hScrollbar = true;
        scrollDirection = 0;
        scrollPosition = 0;
        scrollSpeed = 30;
        size = 10;
        style = "plain";
        vScrollbar = true;
        name = "TextArea";
    }

    public ElementTextArea(WidgetElement widget, String name)
    {
        super(widget);
        alignment = "left";
        bgColor = "#FFFFFF";
        bgOpacity = 100;
        color = "#000000";
        data = "";
        editable = true;
        font = "Helvetica";
        hScrollbar = true;
        scrollDirection = 0;
        scrollPosition = 0;
        scrollSpeed = 30;
        size = 10;
        style = "plain";
        vScrollbar = true;
        this.name = name;
    }

    public String getAlignment()
    {
        return alignment;
    }

    public void setAlignment(String s)
    {
        if(alignment == null || !alignment.equals(s))
        {
            setChanged();
            alignment = s;
        }
    }

    public String getBgColor()
    {
        return bgColor;
    }

    public void setBgColor(String s)
    {
        if(bgColor == null || !bgColor.equals(s))
        {
            bgColor = s;
            setChanged();
        }
    }

    public int getBgOpacity()
    {
        return bgOpacity;
    }

    public void setBgOpacity(int i)
    {
        if(bgOpacity != i)
        {
            bgOpacity = i;
            if(bgOpacity > 100)
                bgOpacity = 100;
            if(bgOpacity < 0)
                bgOpacity = 0;
            setChanged();
        }
    }

    public String getColor()
    {
        return color;
    }

    public void setColor(String s)
    {
        if(color == null || !color.equals(s))
        {
            color = s;
            setChanged();
        }
    }

    public String getDataInternal()
    {
        return data;
    }

    public String getData()
    {
        WidgetTextArea area = (WidgetTextArea)myComponent;
        if(area != null)
            data = area.getText();
        return data;
    }

    public void setData(String s)
    {
        if(data == null || !data.equals(s))
        {
            data = s;
            rebuildFont();
            WidgetTextArea area = (WidgetTextArea)myComponent;
            if(area != null)
                area.updateText();
            setChanged();
        }
    }

    public boolean getEditable()
    {
        return editable;
    }

    public void setEditable(boolean b)
    {
        editable = b;
        WidgetTextArea area = (WidgetTextArea)myComponent;
        if(area != null)
            area.setEditable(b);
        setChanged();
    }

    public String getFont()
    {
        return font;
    }

    public void setFont(String s)
    {
        if(font == null || !font.equals(s))
        {
            font = s;
            rebuildFont();
            setChanged();
        }
    }

    @Override
    public void setHeight(int i)
    {
        if(height != i)
        {
            height = i;
            WidgetTextArea area = (WidgetTextArea)myComponent;
            if(area != null)
                area.updateSize();
            setChanged();
        }
    }

    public boolean getHScrollbar()
    {
        return hScrollbar;
    }

    public void setHScrollbar(boolean b)
    {
        hScrollbar = b;
        WidgetTextArea area = (WidgetTextArea)myComponent;
        if(area != null)
            area.setScrollers();
        setChanged();
    }

    public int getScrollDirection()
    {
        return scrollDirection;
    }

    public void setDcrollDirection(int i)
    {
        scrollDirection = i;
    }

    public int getScrollPosition()
    {
        return scrollPosition;
    }

    public void setScrollPosition(int i)
    {
        scrollPosition = i;
    }

    public int getScrollSpeed()
    {
        return scrollSpeed;
    }

    public void setScrollSpeed(int i)
    {
        scrollSpeed = i;
    }

    public int getSize()
    {
        return size;
    }

    public void setSize(int i)
    {
        if(size != i)
        {
            size = i;
            rebuildFont();
            setChanged();
        }
    }

    public String getStyle()
    {
        return style;
    }

    public void setStyle(String s)
    {
        if(style == null || !style.equals(s))
        {
            style = s;
            rebuildFont();
            setChanged();
        }
    }

    public boolean getVScrollbar()
    {
        return vScrollbar;
    }

    public void setVScrollbar(boolean b)
    {
        vScrollbar = b;
        WidgetTextArea area = (WidgetTextArea)myComponent;
        if(area != null)
            area.setScrollers();
        setChanged();
    }

    @Override
    public void setWidth(int i)
    {
        if(width != i)
        {
            width = i;
            WidgetTextArea area = (WidgetTextArea)myComponent;
            if(area != null)
                area.updateSize();
            setChanged();
        }
    }

    public Font getFontObject()
    {
        return fontObject;
    }

    public FontMetrics getFontMetrics()
    {
        return fm;
    }

    public void rebuildFont()
    {
        int styleInt = 0;
        if(style.toLowerCase().equals("bold"))
            styleInt = 1;
        else
        if(style.toLowerCase().equals("italic"))
            styleInt = 2;
        fontObject = new Font(font, styleInt, size);
        if(widget != null)
            fm = widget.getParent().getFontMetrics(fontObject);
    }

    public void parse(Node textNode)
        throws Exception
    {
        NamedNodeMap map = textNode.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue(), null);
        }

        NodeList nodes = textNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue(), childNode);
        }

        if(getName() == null || getName().length() == 0)
        {
            widget.getParent().getDebugFrame().addText("SYSTEM:Error:TextArea element is not named");
            setName("Unknown_" + System.currentTimeMillis());
        }
    }

    private void parseHelper(String nodeName, String value, Node childNode)
    {
        if(nodeName.equals("alignment"))
            setAlignment(value);
        else
        if(nodeName.equals("bgColor"))
            setBgColor(value);
        else
        if(nodeName.equals("bgOpacity"))
            setBgOpacity(Integer.parseInt(value));
        else
        if(nodeName.equals("color"))
            setColor(value);
        else
        if(nodeName.equals("cursor"))
            setCursor(value);
        else
        if(nodeName.equals("data"))
            setData(value);
        else
        if(nodeName.equals("font"))
            setFont(value);
        else
        if(nodeName.equals("height"))
            setHeight(Integer.parseInt(value));
        else
        if(nodeName.equals("name"))
            setName(value);
        else
        if(nodeName.equals("opacity"))
            setOpacity(Integer.parseInt(value));
        else
        if(nodeName.equals("owner"))
            setOwner(value);
        else
        if(nodeName.equals("size"))
            setSize(Integer.parseInt(value));
        else
        if(nodeName.equals("string"))
            setData(value);
        else
        if(nodeName.equals("style"))
            setStyle(value);
        else
        if(nodeName.equals("tooltip"))
            setTooltip(value);
        else
        if(nodeName.equals("visible"))
        {
            if(value.equals("true"))
                setVisible(true);
            else
                setVisible(false);
        } else
        if(nodeName.equals("width"))
            setWidth(Integer.parseInt(value));
        else
        if(nodeName.equals("x"))
            setX(Integer.parseInt(value));
        else
        if(nodeName.equals("y"))
            setY(Integer.parseInt(value));
        else
        if(nodeName.equals("hScrollbar"))
        {
            if(value.equals("true"))
                setHScrollbar(true);
            else
                setHScrollbar(false);
        } else
        if(nodeName.equals("vScrollbar"))
        {
            if(value.equals("true"))
                setVScrollbar(true);
            else
                setVScrollbar(false);
        } else
        if(nodeName.equals("editable"))
        {
            if(value.equals("true"))
                setEditable(true);
            else
                setEditable(false);
        } else
        if(nodeName.startsWith("on"))
            addAction(nodeName, addChildAction(nodeName, childNode));
    }

    private String alignment;
    private String bgColor;
    private int bgOpacity;
    private String color;
    private String data;
    private boolean editable;
    private String font;
    private boolean hScrollbar;
    private int scrollDirection;
    private int scrollPosition;
    private int scrollSpeed;
    private int size;
    private String style;
    private boolean vScrollbar;
    public Font fontObject;
    public FontMetrics fm;
}